package com.pactera.madp.cp.controller.oms.marketing;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.dto.marketing.MarketingPptCreateDTO;
import com.pactera.madp.cp.api.dto.marketing.MarketingPptUpdateDTO;
import com.pactera.madp.cp.api.entity.BaseEntity;
import com.pactera.madp.cp.api.entity.marketing.MarketingPptEntity;
import com.pactera.madp.cp.api.vo.marketing.MarketingPptVO;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.service.MarketingPptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * (McMarketingPpt)表控制层
 *
 * @author yushen.qi
 * @since 2021-05-24 18:55:39
 */
@RestController
@RequestMapping("/marketing-ppt")
@Api(value = "marketingPpt", tags = "营销素材-ppt管理")
public class MarketingPptController {

    /**
     * 服务对象
     */
    @Resource
    private MarketingPptService marketingPptService;

    /**
     * 模糊查询
     *
     * @param title
     * @param classification
     * @return
     */
    @GetMapping("/index")
    @ApiOperation(value = "分页或根据名称查询")
    public R<List<MarketingPptVO>> qryPage(String title, Integer classification) {
        return R.ok(marketingPptService.selectAllOrName(title,classification));
    }

    /**
     * 新增
     *
     * @param dto
     * @return
     */
    @SysLog("新增营销素材-ppt")
    @PostMapping
    @ApiOperation(value = "新增")
    public R<Map<String,Integer>> saveArticle(@RequestBody MarketingPptCreateDTO dto) {
        Integer corpId = CorpContextHolder.getCorpId();
        MarketingPptEntity one = marketingPptService.getOne(Wrappers.<MarketingPptEntity>lambdaQuery().eq(MarketingPptEntity::getTitle, dto.getTitle()).eq(BaseEntity::getDelFlag,0).eq(MarketingPptEntity::getCorpId,corpId).eq(MarketingPptEntity::getClassification,dto.getClassification()));
        if(one!=null){
            return R.failed("标题不能重复！");
        }
        return R.ok(marketingPptService.insert(dto));
    }

    /**
     * 编辑
     *
     * @param pptDto
     * @return
     */
    @SysLog("编辑营销素材-ppt")
    @PutMapping
    @ApiOperation(value = "编辑")
    public R<Boolean> putArticle(@RequestBody MarketingPptUpdateDTO pptDto) {
        return marketingPptService.update(pptDto)?R.ok():R.failed("修改失败");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @SysLog("删除营销素材-ppt")
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public R<Boolean> delete(@PathVariable Integer id) {
        return  marketingPptService.remove(id);
    }

    /**
     * 发布ppt
     *
     * @param id
     * @return
     */
    @SysLog("发布营销素材-ppt")
    @GetMapping("/release/{id}")
    @ApiOperation(value = "发布ppt")
    public R<Boolean> releaseExcel(@PathVariable Integer id){
        MarketingPptEntity marketingPptEntity = new MarketingPptEntity();
        marketingPptEntity.setMarketingState(2);
        marketingPptEntity.setUpdateTime(LocalDateTime.now());
        return marketingPptService.update(marketingPptEntity, Wrappers.<MarketingPptEntity>lambdaQuery().eq(MarketingPptEntity::getId,id))?R.ok(null,"发布成功"):R.failed("发布失败");
    }

}
